﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SilkeyERP.UI.Entities.Shared
{
    public partial class ButtonChooser : DevExpress.XtraEditors.XtraUserControl
    {
        public BaseChooser FormChooserType { get; set; }
        public List<dynamic> SelectedItems { get; private set; }

        public delegate void OnSelectedHandler(List<dynamic> SelectedItems);
        public event OnSelectedHandler OnSelected;

        public void ShowChooserForm()
        {
            ShowForm();
        }

        public ButtonChooser()
        {
            InitializeComponent();  
            if(SelectedItems == null)
                SelectedItems = new List<dynamic>();
            
            buttonEdit1.Text = string.Format(Properties.Resources.ButtonChooserSelectedMessage, SelectedItems.Count.ToString());
        }

        private void buttonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            ShowForm();
        }

        private void ShowForm()
        {
            try
            {
                if (FormChooserType == null)
                    throw new InvalidOperationException(Properties.Resources.ButtonChooserNullFormChooserType);

                if (FormChooserType.ShowDialog() == DialogResult.OK)
                {
                    SelectedItems = FormChooserType.SelectedFields;
                    buttonEdit1.Text = string.Format(Properties.Resources.ButtonChooserSelectedMessage, SelectedItems.Count.ToString());
                    OnSelected(SelectedItems);
                }
                else
                {                    
                    buttonEdit1.Text = string.Format(Properties.Resources.ButtonChooserSelectedMessage, SelectedItems.Count.ToString());
                }
            }
            catch
            {
                throw;
            }
        }
    }
}
